package org.jeecg.jt808.operate;


import org.jeecg.jt808.JTT808Encoder;
import org.jeecg.jt808.JTT808Message;
import org.jeecg.jt808.Jt808Control.Jt808CentralControlConfig;
import org.jeecg.jt808.modbus.ModbusParams;
import org.jeecg.jt808.processor.JT808MessageGenerator;
import org.jeecg.jt808.processor.MessageType;
import org.jeecg.jt808.vo.Packet;
import org.springframework.stereotype.Component;

import static org.jeecg.jt808.util.JT808Util.bytesToHexString;



public class SetVolumeMessageGenerator implements JT808MessageGenerator {
    @Override
    public String generateMessage(JTT808Message message) {
        return "";
    }

    @Override
    public String generateMessage(JTT808Message message, Jt808CentralControlConfig config) {
        int volume = config.getVolume();
        if (volume < 0 || volume > 100) {
            throw new IllegalArgumentException("音量值必须在 0-100 之间");
        }
        message.packetCount = 0;
        message.packetIndex = 0;
        message.id = 0x8103; // 设置终端参数

        Packet p = Packet.create(6); // 参数项长度：参数 ID (4 字节) + 参数长度 (1 字节) + 参数值 (1 字节)
        p.addInt(0x0254); // 参数 ID: 音量调节
        /*p.addByte((byte) 1); // 参数长度: 1 字节*/
        p.addByte((byte) volume); // 参数值

        message.body = p.getBytes();
        return bytesToHexString(JTT808Encoder.encode(message));
    }

    @Override
    public String generateMessage(JTT808Message message, ModbusParams params) {
        return "";
    }

    @Override
    public String getType() {
        return MessageType.SET_VOLUME.name();
    }
}
